package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @描述 ： 投核保校验 HCT017
 * @Author :  zlc
 * @Date : 2019/9/24 10:37
 **/
public class Hct017InsuranceStrategy implements InsuranceStrategy {

    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository,
                                                    ProductModel productModel, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        //投保人职业类别
        if (StringUtils.isEmpty(custInfoModel.getAppnt().getOccupationType())) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0026_MESSAGE);
        }
        //被保人信息
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                String type = insurantModel.getOccupationType();
                //与投保人的关系
                String relation = insurantModel.getRelToApp();
                //1.校验职业类别是否填写
                if (StringUtils.isEmpty(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0017_MESSAGE);
                }
                //2.校验投保人和被保人的关系
                if (!(productConfig.getRelationList().contains(relation))) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE);
                }
                //3.被保人理赔标识
                if (insurantModel.getClaimFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0027,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0027_MESSAGE,insurantModel.getName()));
                }
                //4.非HCT011的险种，职业类别不能为5、6、7类
                if (productConfig.getInsurantTypeList().contains(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, String.format(UnderWriteConstant.UNDER_WRITE_NPGG0016_MESSAGE,type));
                }
                //5.黑名单
                if (insurantModel.getBlackListFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0031,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0031_MESSAGE,insurantModel.getName()));
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);

    }

}
